package com.ethan.commons.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * 
 * @author ethan.fang
 *
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class BaseDao extends SqlMapClientDaoSupport {

	private static final Log log = LogFactory.getLog(BaseDao.class);

	/*
	 * 加载单条记录
	 */
	public Object load(String statementName, Object paramMap) {
		log.debug("ibatis load " + statementName);
		return getSqlMapClientTemplate()
				.queryForObject(statementName, paramMap);
	}

	/*
	 * 可用于计数
	 */
	public int queryForInt(String statementName, Object paramMap) {
		Integer rst = (Integer) load(statementName, paramMap);
		return rst == null ? 0 : rst.intValue();
	}

	/*
	 * 查询文本
	 */
	public String queryForStr(String statementName, Object paramMap) {
		return (String) load(statementName, paramMap);
	}

	/*
	 * (non-Javadoc)
	 */
	public List queryForList(String statementName, Object paramObj) {
		return this.getSqlMapClientTemplate().queryForList(statementName,
				paramObj);
	}

	/*
	 * add
	 */
	public Object insert(String statementName, Object paramMap) {
		log.debug("ibatis insert " + statementName);
		return getSqlMapClientTemplate().insert(statementName, paramMap);
	}

	/*
	 * update
	 */
	public boolean update(String statementName, Object paramMap) {
		log.debug("ibatis update " + statementName);
		return this.getSqlMapClientTemplate().update(statementName, paramMap) > 0;
	}

	/*
	 * delete
	 */
	public boolean delete(String statementName, Object paramMap) {
		log.debug("ibatis delete " + statementName);
		return getSqlMapClientTemplate().delete(statementName, paramMap) > 0;
	}

	/**
	 * batch add
	 * 
	 * @param statementName
	 * @param datas
	 * @return
	 */
	public boolean batch4Update(final String statementName,
			final List<Object> datas) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (Object object : datas) {
					executor.insert(statementName, object);
				}
				executor.executeBatch();
				return null;
			}
		});
		return true;
	}

	/**
	 * batch insert
	 * 
	 * @param statementName
	 * @param datas
	 * @param batchSize
	 * @return
	 */
	public boolean batch4Update(final String statementName,
			final List<Object> datas, final int batchSize) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				int batch = 0;
				for (Object object : datas) {
					executor.insert(statementName, object);
					batch++;
					if (batch > batchSize) {
						batch = 0;
						executor.executeBatch();
					}
				}
				if (batch > 0)
					executor.executeBatch();
				return null;
			}
		});
		return true;
	}

}